package me.cxlr.qinlauncher2.adapter.drawer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import me.cxlr.qinlauncher2.R;
import me.cxlr.qinlauncher2.adapter.NeedFlushCallBack;
import me.cxlr.qinlauncher2.manager.IconManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.App;
import me.cxlr.qinlauncher2.util.DisplayUtil;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class FolderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Activity activity;
    private final Context context;
    private final List<App> appInFolderList;

    public FolderAdapter(Activity activity, Context context, List<App> appInFolderList) {
        this.activity = activity;
        this.context = context;
        this.appInFolderList = appInFolderList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_folder_grid, parent, false);
        return new AppFolderGridViewHolder(activity, context, view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AppFolderGridViewHolder viewHolder = (AppFolderGridViewHolder) holder;
        View view = viewHolder.view;
        viewHolder.appInFolder = appInFolderList.get(position);
        viewHolder.tvLabel.setText(viewHolder.appInFolder.getLabel());
        viewHolder.setIconAndLabelSize(context);
        viewHolder.llcItem.setOnClickListener(v -> SoftwareManager.getInstance().openApplication(activity, context, appInFolderList.get(position).getPackageName(), appInFolderList.get(position).getClazzName()));
        viewHolder.llcItem.setOnLongClickListener(v -> {
            viewHolder.setMenu(position, appInFolderList.size() - 1, appInFolderList.size(), needFlushCallBack);
            return false;
        });

        Drawable icon = IconManager.getInstance().getApplicationIcon(viewHolder.appInFolder.getPackageName(), viewHolder.appInFolder.getClazzName());
        viewHolder.imgIcon.setImageDrawable(icon);
    }

    @Override
    public int getItemCount() {
        return appInFolderList.size();
    }

    private NeedFlushCallBack needFlushCallBack;

    public void setFlush(NeedFlushCallBack needFlushCallBack) {
        this.needFlushCallBack = needFlushCallBack;
    }

    public static class AppFolderGridViewHolder extends RecyclerView.ViewHolder {
        public final Activity activity;
        public final Context context;
        public final View view;
        public final LinearLayoutCompat llcItem;
        public final AppCompatImageView imgIcon;
        public final AppCompatTextView tvLabel;

        public App appInFolder;

        public AppFolderGridViewHolder(Activity activity, Context context, View view) {
            super(view);
            this.activity = activity;
            this.context = context;
            this.view = view;
            llcItem = view.findViewById(R.id.iafg_ll_item);
            imgIcon = view.findViewById(R.id.iafg_img_icon);
            tvLabel = view.findViewById(R.id.iafg_tv_label);
        }

        /**
         * 设置App的图标和标题大小
         *
         * @param context 上下文
         */
        public void setIconAndLabelSize(Context context) {
            float size = Float.parseFloat(SharedPreferencesUtil.getInstance().getString("app_icon_size", "66"));
            float labelTextSize = Float.parseFloat(SharedPreferencesUtil.getInstance().getString("app_label_size", "20"));

            ViewGroup.LayoutParams params = imgIcon.getLayoutParams();
            params.height = DisplayUtil.dpToPx(context, size);
            params.width = DisplayUtil.dpToPx(context, size);
            imgIcon.setLayoutParams(params);

            // ViewGroup.LayoutParams params1 = tvLabel.getLayoutParams();
            // params1.width = DisplayUtil.dpToPx(context, (int) (size * 2.1));
            // tvLabel.setLayoutParams(params1);
            tvLabel.setTextSize(labelTextSize);
        }

        /**
         * 设置App的菜单
         */
        @SuppressLint("NonConstantResourceId")
        private void setMenu(int position, int lastPosition, int size, NeedFlushCallBack needFlushCallBack) {
            PopupMenu popupMenu = new PopupMenu(context, view);
            if (position == 0 && size != 1) {
                popupMenu.getMenuInflater().inflate(R.menu.menu_app_folder_2, popupMenu.getMenu());
            } else if (position == 0) {
                popupMenu.getMenuInflater().inflate(R.menu.menu_app_folder_4, popupMenu.getMenu());
            } else if (position == lastPosition) {
                popupMenu.getMenuInflater().inflate(R.menu.menu_app_folder_3, popupMenu.getMenu());
            } else {
                popupMenu.getMenuInflater().inflate(R.menu.menu_app_folder_1, popupMenu.getMenu());
            }
            popupMenu.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()) {
                    case R.id.maf_open_app:
                        SoftwareManager.getInstance().openApplication(activity, context, appInFolder.getPackageName(), appInFolder.getClazzName());
                        break;
                    case R.id.mad_remove_app:
                        SoftwareManager.getInstance().uninstallApplication(activity, context, appInFolder.getPackageName());
                        break;
                    case R.id.mad_app_message:
                        SoftwareManager.getInstance().openApplicationMessage(activity, context, appInFolder.getPackageName());
                        break;
                    case R.id.mad_app_up:
                        needFlushCallBack.flush("up", appInFolder, appInFolder.getAppDrawer().getFolder());
                        break;
                    case R.id.mad_app_down:
                        needFlushCallBack.flush("down", appInFolder,  appInFolder.getAppDrawer().getFolder());
                        break;
                    case R.id.mad_app_out_folder:
                        needFlushCallBack.flush("out", appInFolder,  appInFolder.getAppDrawer().getFolder());
                        break;
                    default:
                        break;
                }
                return false;
            });
            popupMenu.show();
        }
    }
}
